<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="get">GET</button>
    <button id="post">POST</button>


    <script>
        document.getElementById('get').addEventListener('click',()=>{
            const xhr = new XMLHttpRequest()
            xhr.open('get','http://127.0.0.1/api/get?id=1&name=张扬')
            xhr.send()
            xhr.onreadystatechange = ()=>{
                if(xhr.readyState === 4){
                    if(xhr.status>=200&&xhr.status<400){
                        console.log(xhr.response)
                    }
                }
            }
        })
        
        document.getElementById('post').addEventListener('click',()=>{
            const obj = {
                id:'1',
                name:'张扬',
                age:'22'
            }
            const xhr = new XMLHttpRequest()
            xhr.open('post','http://127.0.0.1/api/post')
            xhr.send(JSON.stringify(obj))
            xhr.onreadystatechange = ()=>{
                if(xhr.readyState === 4){
                    if(xhr.status>=200&&xhr.status<400){
                        console.log(xhr.responseText)
                    }
                }
            }
        })
    </script>
</body>
</html>